/****************************************************
 * Description: DAOimpl for 角色
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
    *  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuolframework.security.dao.hibernate;

import java.util.List;

import com.bfsuolframework.security.entity.Role;
import org.springframework.stereotype.Repository;

import com.bfsuolframework.security.dao.RoleDAO;
import com.bfsuolframework.core.controller.support.Pagination;
import com.bfsuolframework.core.dao.HibernateDAOSupport;
import com.bfsuolframework.core.dao.support.HqlParameter;

@Repository
public class RoleDAOImpl extends HibernateDAOSupport<Role> implements RoleDAO {

    //通过 用户主键 查询 角色 集合
    private final String FIND_ROLE_BYUSERID = "FROM Role role WHERE role.users.id = :userId";
    
    //按roleName模糊查询不再用户组（groupId）里的角色 集合
    private final String FIND_ROLE_NOT_GROUP = "FROM Role role WHERE role.title like :roleName and role.id not in (SELECT t.role.id FROM GroupRole t WHERE t.group.id = :groupId)";
	
	// 校验角色代码是否唯一
    private final String CHECK_ROLE_CODE = "select count(*) from Role r where r.code=:code";
    
    // 检验是否有关联的用户组
    private final String CHECK_ROLE_USER = "select count(*) RecCount from t_sec_r_user_role WHERE role_id=";
    private final String CHECK_ROLE_GROUP = "select count(*) from GroupRole gr where gr.role.id=:rid";
    private final String CHECK_ROLE_PRI = "select count(*) from RolePrivilege rp where rp.role.id=:rid";
    //根据角色编码获取角色
    private final String GET_BY_CODE="from Role r where r.code=:code";
    /**
     * 通过 用户主键 查询 角色
     * @return 角色 集合
     */
    public List<Role> findRolesByUserId(Long userId){
        return findAll(FIND_ROLE_BYUSERID, HqlParameter.newInstance("userId", userId));
    }

    
	/* (non-Javadoc)
	 * @see RoleDAO#findRolesNotInGroup(java.lang.Long, java.lang.String, com.bfsuolframework.core.controller.support.Pagination)
	 */
	public Pagination findRolesNotInGroup(Long groupId, String roleName,
			Pagination page) {
		
		return getPage(FIND_ROLE_NOT_GROUP, HqlParameter.newInstance("groupId", groupId).addParameter("roleName", "%"+roleName+"%"),page);
	}
	
	/* (non-Javadoc)
	 * @see RoleDAO#checkCode(java.lang.String)
	 */
	public boolean checkCode(Role role){
		if(role.isNew()){
			return getCount(CHECK_ROLE_CODE, HqlParameter.newInstance("code",role.getCode())) == 0;
		}else{
			return getCount(CHECK_ROLE_CODE + " and r.code !=:code", HqlParameter.newInstance("code",role.getCode())) == 0;
		}
	}
	
	/* (non-Javadoc)
	 * @see RoleDAO#checkUsers(java.lang.Long)
	 */
	public boolean checkUsers(Long id){
		return getCountBySQL(CHECK_ROLE_USER + id) == 0;
	}
	
	/* (non-Javadoc)
	 * @see RoleDAO#checkGroups(java.lang.Long)
	 */
	public boolean checkGroups(Long id){
		return getCount(CHECK_ROLE_GROUP, HqlParameter.newInstance("rid",id)) == 0;
	}
	
	/* (non-Javadoc)
	 * @see RoleDAO#checkPris(java.lang.Long)
	 */
	public boolean checkPris(Long id){
		return getCount(CHECK_ROLE_PRI, HqlParameter.newInstance("rid",id)) == 0;
	}

	/*
	 * (non-Javadoc)
	 * @see RoleDAO#getByCode(java.lang.String)
	 */
	public Role getByCode(String roleCode) {
		return getByHql(GET_BY_CODE, HqlParameter.newInstance("code",roleCode));
	}

}